package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/user/shoppingCart")
@RestController
@Slf4j
@Api(tags = "购物车管理")
public class ShoppingCatController {

    @Autowired
    private ShoppingCatService shoppingCatService;

    /*添加购物车*/
    @PostMapping("/add")
    @ApiOperation(value = "添加购物车")
    public Result addShoppingCat(@RequestBody ShoppingCartDTO shoppingCatDTO) {
        log.info("添加购物车,{}", shoppingCatDTO);
        shoppingCatService.addShoppingCat(shoppingCatDTO);
        return Result.success();
    }

    /*查询购物车*/
    @GetMapping("/list")
    @ApiOperation(value = "查询购物车")
    public Result<List<ShoppingCart>> list() {
        log.info("查询购物车");
        List<ShoppingCart> list = shoppingCatService.showShoppingCat();
        return Result.success(list);
    }

    /*清空购物车*/
    @DeleteMapping("/clean")
    @ApiOperation(value = "清理购物车", httpMethod = "DELETE")
    public Result clean() {
        log.info("清空购物车");
        shoppingCatService.deleteShoppingCat();
        return Result.success();
    }

    /*删除购物车中的某个商品*/
    @PostMapping("/sub")
    @ApiOperation(value = "删除购物车中的一个商品", httpMethod = "POST")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCatDTO) {
        log.info("删除购物车中的一个商品,{}", shoppingCatDTO);
        shoppingCatService.sub(shoppingCatDTO);
        return Result.success();
    }
}